package com.agileai.hotweb.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.agileai.domain.DataParam;
import com.agileai.hotweb.common.HandlerParser;
import com.agileai.hotweb.controller.core.BaseHandler;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.hotweb.servlet.DispatchServlet;

public class SimpleDispatchServlet extends DispatchServlet {
	private static final long serialVersionUID = 1L;
	protected Logger LOG = Logger.getLogger(SimpleDispatchServlet.class);

	protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		log.debug("DispatchServlet------start");
		try {
			ViewRenderer viewRenderer = null;
			String handlerId = parseHandlerId(request);
			BaseHandler handler = HandlerParser.getOnly().instantiateHandler(handlerId);
			handler.setDispatchServlet(this);
			handler.setRequest(request);
			handler.setResponse(response);
			DataParam param = new DataParam(request.getParameterMap());
			viewRenderer = handler.processRequest(param);
			viewRenderer.setHandler(handler);
			viewRenderer.executeRender(this, request, response);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.debug("DispatchServlet------end");
	}
}